import air.update.ApplicationUpdaterUI;
import air.update.events.UpdateEvent;

import com.codezen.component.downloadmanager.CzDownloadManager;
import com.codezen.helper.DatabaseAsync;
import com.codezen.helper.Settings;
import com.codezen.helper.Worker;

import flash.net.URLLoader;
import flash.net.URLRequest;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.managers.PopUpManager;
import mx.utils.ObjectUtil;

import utils.serials.Episode;
import utils.serials.EpisodesDB;

[Bindable]
private var serials:ArrayCollection;
[Bindable]
private var videos:ArrayCollection;

private var login_sd:String = "";
private var pass_sd:String = "";
private var login_hd:String = "";
private var pass_hd:String = "";

[Bindable]
private var rusd:Boolean;
[Bindable]
private var ruhd:Boolean;

private var divider_pos:int = 50;

private var request:URLRequest;
private var loader:URLLoader;

private var sets:Settings;
private var settings:Array;

private function initApp():void{
	// check update
	//var up:UpdateManager = new UpdateManager("http://rushdtv.googlecode.com/hg/src/update.xml",false,true);
	var up:ApplicationUpdaterUI = new ApplicationUpdaterUI();
	up.updateURL = "http://rushdtv.googlecode.com/hg/src/update.xml";
	up.isCheckForUpdateVisible = false;
	up.addEventListener(UpdateEvent.INITIALIZED, function():void{
		up.checkNow();
	});
	up.initialize();
	
	// init database
	//initDatabase();
	
	// load prefs
	sets = new Settings("settings.xml");
	loadPrefs();
}

private function loadPrefs():void{
	settings = sets.loadPrefs();
	
	login_sd = settings['login_sd'];
	pass_sd = settings['pass_sd'];
	login_hd = settings['login_hd'];
	pass_hd = settings['pass_hd'];
	if( settings['hd'] == true ){
		ruhd = true;
	}else{
		rusd = true;
	}
		
	// if vars are valid - do login
	if (
		( settings['hd'] == false && login_sd != null && pass_sd != null && login_sd.length > 1 && pass_sd.length > 1) ||
		( settings['hd'] == true && login_hd != null && pass_hd != null && login_hd.length > 1 && pass_hd.length > 1)
		){
		tryLogin();
	}
}

private function save():void{
	settings['login_sd'] = login_sd;
	settings['pass_sd'] = pass_sd;
	settings['login_hd'] = login_hd;
	settings['pass_hd'] = pass_hd;
	settings['hd'] = ruhd_sel.selected;
	
	sets.save(settings);
}

private function onIOError(e:IOErrorEvent):void{
	//dispatchError(e.text, "IO Error happened in MBInfo class");
	Alert.show("Generic IO Error! "+e.text);
}

// advanced garbage collection

/**
 * GC counter 
 */
private var gcCount:int;

/**
 * Starts GC
 */
private function startGCCycle():void{
	gcCount = 0;
	addEventListener(Event.ENTER_FRAME, doGC);
}

/**
 * GC Timer proc, does the collection
 * @param evt
 * 
 */
private function doGC(evt:Event):void{
	flash.system.System.gc();
	if(++gcCount > 1){
		removeEventListener(Event.ENTER_FRAME, doGC);
		setTimeout(lastGC, 40);
	}
}

/**
 * Last GC, to be sure 
 */
private function lastGC():void{
	flash.system.System.gc();
}